//
//  SLBarCodeViewController.m
//  中邮阅读APP
//
//  Created by 康世朋 on 2017/11/20.
//  Copyright © 2017年 康世朋. All rights reserved.
//

#import "SLBarCodeViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "SPToolSet.h"
#import "TMTradeDetailController.h"
@interface SLBarCodeViewController ()<AVCaptureMetadataOutputObjectsDelegate,UIImagePickerControllerDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *scanLineImageView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *containerHeightConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scanLineTopConstraint;
@property (weak, nonatomic) IBOutlet UILabel *customLabel;
@property (weak, nonatomic) IBOutlet UIView *customContainerView;
@property (weak, nonatomic) IBOutlet UILabel *testLabel;

@property (strong, nonatomic) AVCaptureDevice * device;
@property (strong, nonatomic) AVCaptureDeviceInput * input;
@property (strong, nonatomic) AVCaptureMetadataOutput * output;
@property (strong, nonatomic) AVCaptureSession * session;
@property (strong, nonatomic) AVCaptureVideoPreviewLayer * previewLayer;

/*** 专门用于保存描边的图层 ***/
@property (nonatomic,strong) CALayer *containerLayer;

@property (nonatomic, assign) NSInteger itemId;
@property (nonatomic, assign) NSInteger articleId;
@property (nonatomic, assign) BOOL isArticle;

@property (nonatomic, strong) NSString *appKey_QRcode;

@end

static CGFloat width = 230;

@implementation SLBarCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
#if DEBUG
    _testLabel.hidden = NO;
#else
    _testLabel.hidden = YES;
#endif
    // 开始扫描二维码
    [self startScan];
}

- (void)startScan {
    
    // 根据当前选中的按钮重新设置二维码容器高度
    self.containerHeightConstraint.constant = width;
    // 刷新UI
    [self.view layoutIfNeeded];
    
    // 移除动画
    [self.scanLineImageView.layer removeAllAnimations];
    
    [self startAnimation];
    
    // 1.判断输入能否添加到会话中
    if (![self.session canAddInput:self.input]) return;
    [self.session addInput:self.input];
    
    
    // 2.判断输出能够添加到会话中
    if (![self.session canAddOutput:self.output]) return;
    [self.session addOutput:self.output];
    
    // 4.设置输出能够解析的数据类型
    // 注意点: 设置数据类型一定要在输出对象添加到会话之后才能设置
    self.output.metadataObjectTypes = self.output.availableMetadataObjectTypes;
    
    // 5.设置监听监听输出解析到的数据
    [self.output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    
    // 6.添加预览图层
    [self.view.layer insertSublayer:self.previewLayer atIndex:0];
    self.previewLayer.frame = self.view.bounds;
    
    // 7.添加容器图层
    [self.view.layer addSublayer:self.containerLayer];
    self.containerLayer.frame = self.view.bounds;
    
    // 8.开始扫描
    [self.session startRunning];
}

#pragma mark --------AVCaptureMetadataOutputObjectsDelegate ---------
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection {
    // id 类型不能点语法,所以要先去取出数组中对象
    AVMetadataMachineReadableCodeObject *object = [metadataObjects lastObject];
    
    if (object == nil || ![object isKindOfClass:[AVMetadataMachineReadableCodeObject class]]) return;
    [self.session stopRunning];
    TMTradeDetailController *vc = [[UIStoryboard storyboardWithName:@"Mine" bundle:nil] instantiateViewControllerWithIdentifier:@"TMTradeDetailController"];
    [self.navigationController pushViewController:vc animated:YES];
    // 只要扫描到结果就会调用
    //self.customLabel.text = object.stringValue;
   // NSLog(@"二维码扫描结果-->%@",object.stringValue);
#if DEBUG
    _testLabel.text = [NSString stringWithFormat:@"注：该信息只在测试时显示，非bug\n%@", object.stringValue];
#else
    _testLabel.hidden = YES;
#endif
    NSArray<NSString *> *subString = [object.stringValue componentsSeparatedByString:@"?"];
    NSArray *inforArray = [subString.lastObject componentsSeparatedByString:@"|"];
    //if (inforArray.count == 6) {
        _appKey_QRcode = inforArray.lastObject;
//        if ([inforArray.firstObject isEqualToString:@"magazine"]) {
//            BOOL hasArticle = [inforArray[3] isEqualToString:@"1"];
//            [self gotoMagazineWithMagazineId:[inforArray[1] integerValue] hasArticle:hasArticle];
        
       
//        }else {
//            [WZProgressHUD showInfoWithStatus:@"未识别到有效内容，请换个角度重试"];
//            [self.session startRunning];
//        }
//    }else {
//        [WZProgressHUD showInfoWithStatus:@"未识别到有效内容，请换个角度重试"];
//        [self.session startRunning];
//    }
 
    /*
     //清除之前的描边
    [self clearLayers];
     //对扫描到的二维码进行描边
    AVMetadataMachineReadableCodeObject *obj = (AVMetadataMachineReadableCodeObject *)[self.previewLayer transformedMetadataObjectForMetadataObject:object];
    
     //绘制描边
    [self drawLine:obj];
     */
}

#pragma mark - 跳转







#pragma mark - 描边
- (void)drawLine:(AVMetadataMachineReadableCodeObject *)objc {
    NSArray *array = objc.corners;
    
    // 1.创建形状图层, 用于保存绘制的矩形
    CAShapeLayer *layer = [[CAShapeLayer alloc] init];
    
    // 设置线宽
    layer.lineWidth = 2;
    // 设置描边颜色
    layer.strokeColor = [UIColor greenColor].CGColor;
    layer.fillColor = [UIColor clearColor].CGColor;
    
    // 2.创建UIBezierPath, 绘制矩形
    UIBezierPath *path = [[UIBezierPath alloc] init];
    CGPoint point = CGPointZero;
    int index = 0;
    
    CFDictionaryRef dict = (__bridge CFDictionaryRef)(array[index++]);
    // 把点转换为不可变字典
    // 把字典转换为点，存在point里，成功返回true 其他false
    CGPointMakeWithDictionaryRepresentation(dict, &point);
    
    // 设置起点
    [path moveToPoint:point];
    
    // 2.2连接其它线段
    for (int i = 1; i<array.count; i++) {
        CGPointMakeWithDictionaryRepresentation((__bridge CFDictionaryRef)array[i], &point);
        [path addLineToPoint:point];
    }
    // 2.3关闭路径
    [path closePath];
    
    layer.path = path.CGPath;
    // 3.将用于保存矩形的图层添加到界面上
    [self.containerLayer addSublayer:layer];
}

- (void)clearLayers {
    if (self.containerLayer.sublayers) {
        for (CALayer *subLayer in self.containerLayer.sublayers) {
            [subLayer removeFromSuperlayer];
        }
    }
}




#pragma mark -------- 懒加载---------
- (AVCaptureDevice *)device {
    if (_device == nil) {
        _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    }
    return _device;
}

- (AVCaptureDeviceInput *)input {
    if (_input == nil) {
        _input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
    }
    return _input;
}

- (AVCaptureSession *)session {
    if (_session == nil) {
        _session = [[AVCaptureSession alloc] init];
    }
    return _session;
}

- (AVCaptureVideoPreviewLayer *)previewLayer {
    if (_previewLayer == nil) {
        _previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.session];
    }
    return _previewLayer;
}
// 设置输出对象解析数据时感兴趣的范围
// 默认值是 CGRect(x: 0, y: 0, width: 1, height: 1)
// 通过对这个值的观察, 我们发现传入的是比例
// 注意: 参照是以横屏的左上角作为, 而不是以竖屏
//        out.rectOfInterest = CGRect(x: 0, y: 0, width: 0.5, height: 0.5)
- (AVCaptureMetadataOutput *)output {
    if (_output == nil) {
        _output = [[AVCaptureMetadataOutput alloc] init];
    
        // 1.获取屏幕的frame
        CGRect viewRect = self.view.frame;
        // 2.获取扫描容器的frame
        CGRect containerRect = self.customContainerView.frame;
        
        CGFloat x = containerRect.origin.y / viewRect.size.height;
        CGFloat y = containerRect.origin.x / viewRect.size.width;
        CGFloat width = containerRect.size.height / viewRect.size.height;
        CGFloat height = containerRect.size.width / viewRect.size.width;
        
        // CGRect outRect = CGRectMake(x, y, width, height);
        // [_output rectForMetadataOutputRectOfInterest:outRect];
        _output.rectOfInterest = CGRectMake(x, y, width, height);
    }
    return _output;
}

- (CALayer *)containerLayer {
    if (_containerLayer == nil) {
        _containerLayer = [[CALayer alloc] init];
    }
    return _containerLayer;
}

#pragma mark - 动画效果
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.session stopRunning];
    [self.scanLineImageView.layer removeAllAnimations];
}

// 界面显示,开始动画
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.session startRunning];
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self startAnimation];
    
}
// 开启冲击波动画
- (void)startAnimation {
    self.containerHeightConstraint.constant = width;
    // 1.设置冲击波底部和容器视图顶部对齐
    self.scanLineTopConstraint.constant = -self.containerHeightConstraint.constant;
    // 刷新UI
    [self.view layoutIfNeeded];
    
    // 2.执行扫描动画
    [UIView animateWithDuration:1.0 animations:^{
        // 无线重复动画
        [UIView setAnimationRepeatCount:MAXFLOAT];
        self.scanLineTopConstraint.constant = self.containerHeightConstraint.constant;
        // 刷新UI
        [self.view layoutIfNeeded];
    } completion:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    NSLog(@"二维码控制器销毁");
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
